package com.wyp.demo.pattern.proxy;

import com.wyp.demo.pattern.proxy.cglb.CGLIGUtil;
import com.wyp.demo.pattern.proxy.cglb.CommonMethodInteceptor;
import com.wyp.demo.pattern.proxy.dynamic.AlipayInvocationHandler;
import com.wyp.demo.pattern.proxy.dynamic.ProxyUtil;
import com.wyp.demo.pattern.proxy.staticproxy.AliPayToBProxy;
import com.wyp.demo.pattern.proxy.staticproxy.AliPayToCProxy;
import com.wyp.demo.pattern.proxy.staticproxy.IPayment;
import com.wyp.demo.pattern.proxy.staticproxy.impl.AlipayToB;
import com.wyp.demo.pattern.proxy.staticproxy.impl.AlipayToC;
import com.wyp.demo.pattern.proxy.staticproxy.impl.CommonPay;
import net.sf.cglib.proxy.MethodInterceptor;

import java.lang.reflect.InvocationHandler;

/**
 * @author: Oliver
 * @time: 2020/12/20/10:46
 */
public class ProxyTest {
    public static void main(String[] args) {
        IPayment toCProxy = new AliPayToCProxy(new AlipayToC());
        toCProxy.pay();

        IPayment toBProxy = new AliPayToBProxy(new AlipayToB());
        toBProxy.pay();

        System.out.println("--------------------动态代理---------------");
        IPayment alipayToC = new AlipayToC();
        InvocationHandler handler = new AlipayInvocationHandler(alipayToC);
        IPayment payment = ProxyUtil.newProxyInstance(alipayToC, handler);
        payment.pay();

        IPayment alipayToB = new AlipayToB();
        InvocationHandler handlerToB = new AlipayInvocationHandler(alipayToB);
        payment = ProxyUtil.newProxyInstance(alipayToB, handlerToB);
        payment.pay();

        System.out.println("---------------cglib---------------------");

        CommonPay commonPay = new CommonPay();
        MethodInterceptor inteceptor = new CommonMethodInteceptor();
        CommonPay cglibProxy = CGLIGUtil.cglibProxy(commonPay, inteceptor);
        cglibProxy.pay();
    }
}
